package cn.edu.hpu.jie.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import cn.edu.hpu.jie.entity.Login;
import cn.edu.hpu.jie.entity.User;
import cn.edu.hpu.jie.services.LoginService;
import cn.edu.hpu.jie.services.UserService;
import cn.edu.hpu.jie.util.DateUtil;

import com.octo.captcha.module.servlet.image.SimpleImageCaptchaServlet;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class UserAction extends ActionSupport {

	private static final long serialVersionUID = 1L;
	
	private String jcaptcha;
	private String username;
	private String password;
	private Login login;
	
	public String userLogin() throws IOException {
		HttpServletRequest request = ServletActionContext.getRequest();
		if("POST".equals(request.getMethod())) {
			boolean result = SimpleImageCaptchaServlet.validateResponse(request, jcaptcha);
			HttpServletResponse response = ServletActionContext.getResponse();
			response.setContentType("text/html;charset=UTF-8");
			response.setCharacterEncoding("UTF-8");
			PrintWriter out = response.getWriter();
			int coding = 1001;
			if(result) {
				UserService us = new UserService();
				User u = us.checkLogin(username, password, jcaptcha);
				
				if(u != null) {
					coding = 1003;
					Map<String, Object> session = ActionContext.getContext().getSession();
					session.put("user", u);
				} else {
					coding = 1002;
				}
			}
			
			out.print(coding);
			out.flush();
			out.close();
			return null;
		}
		return ERROR;
	}
	
	public String getUserInfo() {
		Map<String, Object> session = ActionContext.getContext().getSession();
		Object curid = session.get("loginid");
		LoginService ls = new LoginService();
		User u = (User) session.get("user");
		if(curid == null) {
			HttpServletRequest request = ServletActionContext.getRequest();
			login = ls.getLastLogin(u.getId());
			
			if(login != null) {
				login.setIp(request.getRemoteAddr());
				login.setTime(DateUtil.getNow());
				login.setNum(login.getNum() + 1);
			} else {
				login = new Login();
				login.setIp(request.getRemoteAddr());
				login.setTime(DateUtil.getNow());
				login.setNum(1);
				login.setUserid(u.getId());
			}
			
			int loginid = ls.insertLogin(login);
			if(loginid == 0) {
				return ERROR;
			}
			session.put("loginid", loginid);
		} else {
			login = ls.getLastLogin(u.getId());
		}
		return SUCCESS;
	}
	
	//set & get
	public void setJcaptcha(String jcaptcha) {
		this.jcaptcha = jcaptcha;
	}
	
	public void setUsername(String username) {
		this.username = username;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public Login getLogin() {
		return login;
	}

	public void setLogin(Login login) {
		this.login = login;
	}
}
